/**
 * @file skoll.tools.confirmwindow.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: skoll.tools.confirmwindow.js 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.tools.confirmwindow.js $
 */

/**
 * Skoll skin modal windows manager.
 */
Skoll.tools.ConfirmWindow = {
	//
	// Protected properties.
//	_closeListeners: [],
	//
	// Public methods.
//	addCloseListener: function(listener) {
//		Skoll.tools.ConfirmWindow._closeListeners.push(listener);
//	},
	closeConfirm: function() {
		Skoll.tools.preventDefault();
		$("#SkollConfirmDialog").dialog("close");
	},
	modal: function(config) {
		if ($("#SkollConfirmDialog").dialog("isOpen")) {
			$("#SkollConfirmDialog").dialog(config);
		}
	},
	openConfirm: function(url, yesCallback, noCallback) {
		Skoll.tools.log("ConfirmWindow.openConfirm(): Url: " + url);
		$.ajax({
			url: url + "&nolayout",
			type: "GET",
			success: function(data) {
				Skoll.tools.ConfirmWindow.openSimpleConfirm(data, yesCallback, noCallback);
			},
			error: function(data) {
				Skoll.tools.log("ConfirmWindow.openConfirm(): Unable to open ': " + url + "'");
				Skoll.tools.log(data);
			}
		});
	},
	openSimpleConfirm: function(message, yesCallback, noCallback) {
		$("body").append('<div id="SkollConfirmDialog"></div>');

		$("#SkollConfirmDialog").html(message);
		if ($("#SkollConfirmDialog").hasClass("Bermuda")) {
			$("#SkollConfirmDialog").removeClass("Bermuda");
		}

		$("#SkollConfirmDialog").dialog({
			close: function(event) {
//				Skoll.tools.ConfirmWindow._fireCloseListeners();
				$(this).dialog("destroy");
				$("#SkollConfirmDialog").remove();
			},
			buttons: {
				No: function() {
					if (typeof noCallback == "function") {
						noCallback();
					} else {
						Skoll.tools.log("There's no callback for button 'no'");
					}
					$(this).dialog("close");
				},
				Yes: function() {
					if (typeof yesCallback == "function") {
						yesCallback();
					} else {
						Skoll.tools.log("There's no callback for button 'yes'");
					}
					$(this).dialog("close");
				}
			},
			closeOnEscape: false,
			modal: true,
			width: "auto"
		});


		$("[modal-title]").each(function() {
			Skoll.tools.ConfirmWindow.modal({
				title: $(this).attr("modal-title")
			});
		});

		setTimeout(function() {
			$("#SkollConfirmDialog").dialog({
				position: "center"
			});
		}, 200);
		setTimeout(function() {
			$("#SkollConfirmDialog").dialog({
				position: "center"
			});
		}, 1000);
	}
//	,
	//
	// Protected methods.
//	_fireCloseListeners: function() {
//		Skoll.tools.ConfirmWindow._closeListeners.forEach(function(listener) {
//			var type = typeof listener;
//
//			if (type == "function") {
//				listener();
//			} else if (type == "string") {
//				Skoll.tools.log(listener);
//			}
//
//			Skoll.tools.ConfirmWindow._closeListeners.splice(Skoll.tools.ConfirmWindow._closeListeners.indexOf(listener), 1);
//		});
//	}
};